//
//  TBRMineController.m
//  TBRentClient
//
//  Created by tonbright on 2018/4/27.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRMineController.h"
#import "TBRinfoHeadCell.h"
#import "TBRInfoBasicCell.h"
#import "TBRLogOutCell.h"
#import "TBRMineModel.h"
#import <YYModel.h>

#import "BaseNavigationController.h"
#import "TBRNewNoticeListC.h"
#import "TBRUnLoginViewC.h"
#import "TBRLoginController.h"

@interface TBRMineController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView      *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)TBRUnLoginViewC *unLoginViewC;
@end

@implementation TBRMineController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if ([[AppSetting shareAppSetting].isLogin isEqualToString:@"1"]) {
        self.unLoginViewC.view.hidden = YES;
    }else {
        self.unLoginViewC.view.hidden = NO;
    }
}
- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    
    //添加tableView
    [self setupTableView];
    
    [self setupUnLoginC];
}

- (void)setupNavItem {
    self.titleText = @"我的";
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.leading.offset(10);
        make.trailing.offset(-10);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRinfoHeadCell" bundle:nil] forCellReuseIdentifier:@"TBRinfoHeadCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRInfoBasicCell" bundle:nil] forCellReuseIdentifier:@"TBRInfoBasicCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRLogOutCell" bundle:nil] forCellReuseIdentifier:@"TBRLogOutCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 10, 0, 10)];
    }
    self.tableView.showsVerticalScrollIndicator = NO;
}

- (void)setupUnLoginC {
    TBRUnLoginViewC *unLoginViewC = [[TBRUnLoginViewC alloc] init];
    self.unLoginViewC = unLoginViewC;
    [self.view addSubview:unLoginViewC.view];
    [self addChildViewController:unLoginViewC];
    [self.unLoginViewC.view makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.leading.trailing.offset(0);
    }];
    __weak typeof(self) weakSelf = self;
    unLoginViewC.unLoginViewBlock = ^{
        
        //跳转到登录页面----
        TBRLoginController *loginC = [[TBRLoginController alloc] init];
        BaseNavigationController *loginNav = [[BaseNavigationController alloc] initWithRootViewController:loginC];
        [weakSelf presentViewController:loginNav animated:YES completion:nil];
    };
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0 || section == 4) {
        return 1;
    }
    NSArray *tmpArray = self.dataArray[section];
    return tmpArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TBRinfoHeadCellId";
    if (indexPath.section == 0) {
        iden = @"TBRinfoHeadCellId";
    }else if (indexPath.section == 4) {
        //退出登录
        iden = @"TBRLogOutCellId";
    }else {
        iden = @"TBRInfoBasicCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if (indexPath.section == 4) {
        //退出登录
    }else {
        TBRInfoBasicCell *infoBasicCell = (TBRInfoBasicCell *)cell;
        NSArray *tmpArray = self.dataArray[indexPath.section];
        TBRMineInfoBasicModel *basicModel = tmpArray[indexPath.row];
        infoBasicCell.basicModel = basicModel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? 186 : 44;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 10)];
    view.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        
    }else if (indexPath.section == 4) {
        //退出登录
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        // app名称
        NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
        __weak typeof(self) weakSelf = self;
        [self showFinishBtnClickAlertShowMessage:[NSString stringWithFormat:@"确定退出%@?",app_Name] block:^{
            weakSelf.unLoginViewC.view.hidden = NO;
            [AppSetting shareAppSetting].isLogin = @"0";
        }];
        
    }else {
        NSArray *tmpArray = self.dataArray[indexPath.section];
        TBRMineInfoBasicModel *basicModel = tmpArray[indexPath.row];
        if (!basicModel.className) {
            return;
        }
        Class ClassName = NSClassFromString(basicModel.className);
        UIViewController *viewC = [[ClassName alloc] init];
        [self.navigationController pushViewController:viewC animated:YES];
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.dataArray = [NSMutableArray array];
    NSArray *tmpArray = @[@[],
                          @[@{@"messageId":@"beToVip",
                              @"imageName":@"",
                              @"titleName":@"成为高级会员,每单享受九折"},
                            ],
                          @[
                            @{@"messageId":@"myVip",
                              @"imageName":@"",
                              @"titleName":@"我的会员",
                              @"className":@"TBRMineVipC"
                              },
                            @{@"messageId":@"myMessage",
                              @"imageName":@"",
                              @"titleName":@"我的消息",
                              @"className":@"TBRNewNoticeListC"},
                            @{@"messageId":@"myKeFu",
                              @"imageName":@"",
                              @"titleName":@"我的客服"},
                            ],
                          @[
                            @{@"messageId":@"law",
                              @"imageName":@"",
                              @"titleName":@"法律条款与隐私政策"},
                            @{@"messageId":@"about",
                              @"imageName":@"",
                              @"titleName":@"关于用车无忧"},
                            @{@"messageId":@"score",
                              @"imageName":@"",
                              @"titleName":@"欢迎评分"}
                            ],
                          @[]
                          ];
    for (NSArray *tmpEnumArray in tmpArray) {
        NSArray *tmpModelArray = [NSArray yy_modelArrayWithClass:[TBRMineInfoBasicModel class] json:tmpEnumArray];
        [self.dataArray addObject:tmpModelArray];
    }
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}
@end
